package yja.gzw.app.fragmentview

import android.content.Intent
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.tencent.imsdk.v2.V2TIMConversation
import com.tencent.imsdk.v2.V2TIMConversationListFilter
import com.tencent.imsdk.v2.V2TIMConversationListener
import com.tencent.imsdk.v2.V2TIMConversationResult
import com.tencent.imsdk.v2.V2TIMManager
import com.tencent.imsdk.v2.V2TIMValueCallback
import com.tencent.qcloud.tuikit.timcommon.util.ThreadUtils.runOnUiThread
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import yja.gzw.app.R
import yja.gzw.app.activity.PinglongzhanshiActivity
import yja.gzw.app.activity.WodehaoyouActiviry
import yja.gzw.app.activity.XiatongxiaoxiActivity
import yja.gzw.app.activity.ZanheshoucangActiviry
import yja.gzw.app.adapter.Haoyouadapter
import yja.gzw.app.adapter.XiaoxiAdapter
import yja.gzw.app.base.BaseFragment
import yja.gzw.app.databinding.FragmentXiaoxiBinding
import yja.gzw.app.model.UserData
import yja.gzw.app.viewmodel.Mainviewmodel


class XiaoxiFragment :BaseFragment<Mainviewmodel, FragmentXiaoxiBinding>(R.layout.fragment_xiaoxi){
    private lateinit var dimingada: Haoyouadapter


    override fun onResume() {
        super.onResume()

        if(UserData.kan){
            binding.zizi.visibility=View.GONE
        }else{
            binding.zizi.visibility=View.VISIBLE

        }

        GlobalScope.launch {

            runOnUiThread {


                V2TIMManager.getConversationManager().getConversationList(0,Integer.MAX_VALUE,object :
                    V2TIMValueCallback<V2TIMConversationResult> {
                    override fun onSuccess(v2TIMConversationResult : V2TIMConversationResult) {

                        Log.e("是否成功","列表展示成功")

                        val v2TIMConversationList = v2TIMConversationResult.conversationList
                        for (v2TIMConversation in v2TIMConversationList) {
                            Log.i("消息列表", "好友名称" + v2TIMConversation.showName)

                        }
                        binding.listshow.layoutManager=
                            LinearLayoutManager(context)
                        dimingada= Haoyouadapter(this@XiaoxiFragment.requireContext(),v2TIMConversationList)
                        binding.listshow.adapter= dimingada




                    }

                    override fun onError(code: Int, desc: String?) {

                        Log.e("错误列表信息",desc!!)

                    }
                })


                var conversationListener: V2TIMConversationListener = object : V2TIMConversationListener() {
                    override fun onSyncServerStart() {
                        Log.i("imsdk", "onSyncServerStart")
                    }

                    override fun onSyncServerFinish() {
                        Log.i("imsdk", "onSyncServerFinish")
                    }

                    override fun onSyncServerFailed() {
                        Log.i("imsdk", "onSyncServerFailed")
                    }

                    override fun onNewConversation(conversationList: List<V2TIMConversation>) {


                    }

                    override fun onConversationChanged(conversationList: List<V2TIMConversation>) {
                        binding.listshow.layoutManager=
                            LinearLayoutManager(context)
                        dimingada= Haoyouadapter(this@XiaoxiFragment.requireContext(),conversationList)
                        binding.listshow.adapter= dimingada
                    }

                    override fun onConversationDeleted(conversationIDList: List<String>) {
                        Log.i("imsdk", "onConversationDeleted")
                    }

                    override fun onTotalUnreadMessageCountChanged(totalUnreadCount: Long) {
                        Log.i("imsdk", "onTotalUnreadMessageCountChanged")
                    }

                    override fun onUnreadMessageCountChangedByFilter(
                        filter: V2TIMConversationListFilter,
                        totalUnreadCount: Long
                    ) {
                        Log.i("imsdk", "onUnreadMessageCountChangedByFilter")
                    }
                }
                V2TIMManager.getConversationManager().addConversationListener(conversationListener);

            }}






    }


    // 监听会话的回调



    override fun initView() {

        if(UserData.kan){
            binding.zizi.visibility=View.GONE
        }else{
            binding.zizi.visibility=View.VISIBLE

        }

        viewmodel.onxitongxiaoxiwang(UserData.token)
        binding.xitongxiangxi.setOnClickListener {
            val intent=Intent(context, XiatongxiaoxiActivity::class.java)
            startActivity(intent)
        }




        GlobalScope.launch {

            runOnUiThread {


                V2TIMManager.getConversationManager().getConversationList(0,Integer.MAX_VALUE,object :
                    V2TIMValueCallback<V2TIMConversationResult> {
                    override fun onSuccess(v2TIMConversationResult : V2TIMConversationResult) {

                        Log.e("是否成功","列表展示成功")

                        val v2TIMConversationList = v2TIMConversationResult.conversationList
                        for (v2TIMConversation in v2TIMConversationList) {
                            Log.i("消息列表", "好友名称" + v2TIMConversation.showName)

                        }
                        binding.listshow.layoutManager=
                            LinearLayoutManager(context)
                        dimingada= Haoyouadapter(this@XiaoxiFragment.requireContext(),v2TIMConversationList)
                        binding.listshow.adapter= dimingada

                    }

                    override fun onError(code: Int, desc: String?) {

                        Log.e("错误列表信息",desc!!)

                    }
                })


            }}
        binding.zangeshoucang.setOnClickListener {
            val inten=Intent(context, ZanheshoucangActiviry::class.java)
            context?.startActivity(inten)
        }

        binding.xingzengguanzhu.setOnClickListener {

            val intent=Intent(context, WodehaoyouActiviry::class.java)
            context?.startActivity(intent)
        }
        binding.pinglun.setOnClickListener {
            val inten=Intent(context, PinglongzhanshiActivity::class.java)
            context?.startActivity(inten)


        }

    }

    override fun dataObservese() {
        viewmodel.onxitongxiaoxi.observe(this, Observer {
            if(it.code==1){
                if(it.data.data.size==0){
                    binding.zizi.visibility= View.GONE
                }
                binding.xiaoxishow.text=it.data.data[0].content


            }else{
                Toast.makeText(context,it.msg, Toast.LENGTH_SHORT).show()

            }

        })
    }
}